﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gestion_Api.AccesoDatos;
using System.Data.SqlClient;

namespace Escuela.Forms
{
    public partial class NotificacionesForm : Form
    {
        Instituto inst;
        public NotificacionesForm(Instituto inst)
        {
            InitializeComponent();
            this.inst = inst;
        }

        private void NotificacionesForm_Load(object sender, EventArgs e)
        {

        }

        

        private void EnviarMails(int alumno, int profesor)
        {
            if(alumno == 1)
            {
                DataTable dt = this.obtenersMailAlumnos();
                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        string mail = dr[0].ToString();
                        this.inst.enviarMailNotificacion(mail, this.textBox1.Text, this.textBox2.Text);
                    }
                    catch { }
                }    
            }

            if(profesor == 1)
            {
                DataTable dt = this.obtenersMailProfesores();
                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        string mail = dr[0].ToString();
                        this.inst.enviarMailNotificacion(mail, this.textBox1.Text, this.textBox2.Text);
                    }
                    catch { }
                }
            }
            

            //dt = ac.execDT(query);

            
        }

        private DataTable obtenersMailAlumnos()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            AccesoDB ac = new AccesoDB();
            string query = "coop_alumno_GetMAils ";
            

            SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            dt = ac.execDT(command);
            return dt ;
        }

        private DataTable obtenersMailProfesores()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            AccesoDB ac = new AccesoDB();
            string query = "coop_profesor_GetMAils ";
            

            SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            dt = ac.execDT(command);
            return dt ;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int envioAlumnos= 0;
                int ENVIOpROFESOR=0;
                if(this.checkBox1.Checked)
                {
                    envioAlumnos = 1;
                }
                if(this.checkBox2.Checked)
                {
                    ENVIOpROFESOR=1;
                }

                this.EnviarMails(envioAlumnos, ENVIOpROFESOR);
                MessageBox.Show("Notificacion enviada con exito", "Error");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enviando mail de notificacion", "Error");
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
